#include "WordObj.h"


WordObjPtr createWordObj(const char *word, const unsigned long int frequency){
	WordObjPtr newObject = (WordObjPtr) malloc (sizeof(WordObj));
	newObject->frequency = frequency;
	newObject->word = (char *) malloc(sizeof(char)*(strlen(word)+1));
	strncpy(newObject->word, word, strlen(word)+1); 
	return newObject;
}




char *toString(const void * obj){
	WordObjPtr myobj = (WordObjPtr) obj;
	int maxLength = strlen(myobj->word)+1+MAX_WORD_LENGTH+MAX_FREQ_DIGITS+4;
	char *temp = (char *) malloc(sizeof(char) * maxLength);
	snprintf(temp, maxLength, "%s %lu", myobj->word, myobj->frequency);
	return temp;
}




void freeWordObj(const void * obj){
	WordObjPtr myobj = (WordObjPtr) obj;
	if (myobj == NULL) return;
	free(myobj->word);
	free(myobj);
}




Boolean equals(const void * obj1, const void * obj2){
	WordObjPtr o1 = (WordObjPtr) obj1;
	char * o2 = (char *) obj2;
	
	
	//Compare the strings (words)
	if(strcmp(o1->word,o2) == 0){
	    return 1;
	}
	else{
	    return 0;
	}
	
}